package gregorytersoff_controller;

import gregorytersoff_boundary.DecisionlinesGUI;
import gregorytersoff_model.Model;

public class ChoiceNameController {

	
	private DecisionlinesGUI frame;
	private Model model;
	private String newname;
	/**
	 * Checks whether or not the string given is stored in exactly one JTextField. If it is, it prevents further editing of that JTextField and
	 * resets the error message to the default, and if not, it resets the error message to inform the user that the name needs to be changed 
	 * @param text the string to check
	 * @param f the frame to check the JTextFields of
	 * @param m The model, currently unused but left in due to a rapidly approaching deadline and a desire to not break what works when adding in documentation with only 3 hours left
	 */
	public ChoiceNameController (String text, DecisionlinesGUI f, Model m)
	{
		frame = f;
		model = m;
		newname = text;
	}
	
	/**
	 * Checks whether the string given during creation is valid
	 */
	public void process()
	{
		int which = -1;
		boolean ok = true;
		for(int i = 0; i<frame.getChoiceText().length; i++)
		{
			if(newname.equals(frame.getChoiceText()[i].getText()))
			{
				if(which == -1)
				{
					which = i;
					
				}
				else
				{
					new ErrorController("That name is already in use", frame).process(); 
					ok = false;
				}
			}
		}
		if(ok)
		{
			frame.setLocked(which);
			new ErrorController("Any errors will appear here", frame).process();
		}
	}
}
